package com.doaction.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

public class UserSignupPanel extends Composite implements ClickHandler {

	private Label firstNameLabel = new Label("First Name");
	private TextBox firstName = new TextBox();
	private Label lastNameLabel = new Label("Last Name");
	private TextBox lastName = new TextBox();
	private Label emailLabel = new Label("Email");
	private TextBox email = new TextBox();
	private Label passwordLabel = new Label("Password");
	private PasswordTextBox password = new PasswordTextBox();
	private Label confirmPasswordLabel = new Label("Confirm Password");
	private PasswordTextBox confirmPassword = new PasswordTextBox();
	private CheckBox newsletterCheckBox = new CheckBox(
			"Check to subscribe in newsletter");
	private VerticalPanel panel = new VerticalPanel();
	// private FormPanel signupForm = new FormPanel();
	private Button submitButton = new Button("Sign up", this);

	// private String handlerServlet;

	public UserSignupPanel() {
		// this.handlerServlet = handlerServlet;
		// signupForm.setAction(GWT.getModuleBaseURL() + handlerServlet);
		// signupForm.setMethod(FormPanel.METHOD_POST);
		// signupForm.setWidget(panel);
		// Add an event handler to the form.
		// signupForm.addSubmitHandler(new FormPanel.SubmitHandler() {
		// public void onSubmit(SubmitEvent event) {
		// // This event is fired just before the form is submitted. We can
		// // take
		// // this opportunity to perform validation.
		// /*if (firstName.getText().length() == 0
		// || lastName.getText().length() == 0
		// || email.getText().length() == 0
		// || password.getText().length() == 0
		// || confirmPassword.getText().length() == 0
		// || !password.getText()
		// .equals(confirmPassword.getText())) {
		// Window.alert("Error");
		// event.cancel();
		// }*/
		// }
		// });

		// signupForm
		// .addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
		// public void onSubmitComplete(SubmitCompleteEvent event) {
		// // When the form submission is successfully completed,
		// // this
		// // event is
		// // fired. Assuming the service returned a response of
		// // type
		// // text/html,
		// // we can get the result text here (see the FormPanel
		// // documentation for
		// // further explanation).
		// Window.alert(event.getResults());
		// }
		// });

		panel.add(firstNameLabel);
		panel.add(firstName);
		panel.add(lastNameLabel);
		panel.add(lastName);
		panel.add(emailLabel);
		panel.add(email);
		panel.add(passwordLabel);
		panel.add(password);
		panel.add(confirmPasswordLabel);
		panel.add(confirmPassword);
		panel.add(newsletterCheckBox);
		panel.add(submitButton);
		newsletterCheckBox.addClickHandler(this);

		// All composites must call initWidget() in their constructors.
		initWidget(panel);
	}

	@Override
	public void onClick(ClickEvent event) {
		Object sender = event.getSource();
		if (sender == newsletterCheckBox) {
			// enroll in newsletter if checked
			if (newsletterCheckBox.getValue())
				Window.alert("enrolled in newsletter updates");
			else
				Window.alert("removed from newsletter updates");
		} else if (sender == submitButton) {
			// AddUserServiceAsync service = (AddUserServiceAsync) GWT
			// .create(AddUserService.class);
			// ServiceDefTarget serviceDef = (ServiceDefTarget) service;
			// serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
			// + handlerServlet);
			// service.addUser("", "", "", "", true, new AddUserCallback() {
			// });
			// signupForm.submit();
			// RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,
			// GWT.getModuleBaseURL() + handlerServlet);
			// try {
			//				
			//				
			//				
			// Request request = builder.sendRequest(null,
			// new RequestCallback() {
			// public void onError(Request request,
			// Throwable exception) {
			// Window.alert("error");
			// }
			//
			// public void onResponseReceived(Request request,
			// Response response) {
			// if (200 == response.getStatusCode()) {
			// Window.alert(response.getText());
			// } else {
			// Window.alert("Couldn't retrieve JSON ("
			// + response.getStatusText() + ")");
			// }
			// }
			// });
			// } catch (RequestException e) {
			// 
			// e.printStackTrace();
			// }

			AddUserServiceAsync service = GWT.create(AddUserService.class);
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

				@Override
				public void onFailure(Throwable caught) {					
					Window.alert("failure");
				}

				@Override
				public void onSuccess(Boolean result) {
					Window.alert("Wsuccess");
				}

			};

			if (firstName.getText().length() == 0
					|| lastName.getText().length() == 0
					|| email.getText().length() == 0
					|| password.getText().length() == 0
					|| confirmPassword.getText().length() == 0
					|| !password.getText().equals(confirmPassword.getText())) {
				Window.alert("Error");
				return;
			}

			service
					.addUser(firstName.getText(), lastName.getText(), email
							.getText(), password.getText(), newsletterCheckBox
							.getValue(), callback);
		}
	}
}
